package com.tledu.jdbc;

import java.sql.*;

public class Jdbc3 {
    // 今后开发过程中，大概率会还是用数据持久层框架mybatis
    // 连接池，自动封装 ...
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 加载mysql jdbc的驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 出现乱码一定是编码方式的问题
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/shengchanshixi?characterEncoding=utf-8",
                "root",
                "root"
        );
        String sql = "select * from student where school_name like ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        // 设置参数
        preparedStatement.setString(1, "%经贸%");
        // 执行查询操作
        // executeQuery执行查询操作
        ResultSet rs = preparedStatement.executeQuery();
        // ResultSet可以通过迭代器的方式进行遍历
        while (rs.next()) {
            System.out.print(rs.getString("name") +" ");
            System.out.print(rs.getInt("age")+" ");
            System.out.print(rs.getString("school_name")+" ");
            System.out.print("\n");
        }
        // 关闭这些连接
        rs.close();
        preparedStatement.close();
        connection.close();
    }
}
